/*
 * Droplet.cpp
 *
 * Created: 5/11/2012 4:13:57 PM
 *  Author: common
 */ 

#include <avr/io.h>
#include <math.h>
#include <util/delay.h>

#include "motor.h"
#include "RGB_LED.h"
#include "pc_com.h"
#include "RGB_sensor.h"

extern uint8_t rsenbase,gsenbase,bsenbase;

void Config32MHzClock(void)
{
	// initialize clock source to be 32MHz internal oscillator (no PLL)
	CCP = CCP_IOREG_gc;							//Security Signature to modify clock
	OSC.CTRL = OSC_RC32MEN_bm;					// enable internal 32MHz oscillator
	while(!(OSC.STATUS & OSC_RC32MRDY_bm));		// wait for oscillator ready
	CCP = CCP_IOREG_gc;							//Security Signature to modify clock
	CLK.CTRL = 0x01;							//select sysclock 32MHz osc
}


int main(void)
{
	Config32MHzClock();
	
	motor_init();
	RGB_LED_init();
	rgb_sensor_init();
	pc_com_init();
	
	rgb_sensor_enable();
	
	uint8_t r,g,b;
	while (1)	   
	{			   
		set_red_led(50); _delay_ms(100); led_off();
		r = get_red_sensor(); g = get_green_sensor(); b = get_blue_sensor();
		printf("Sensors: Red %i, Green %i, Blue %i \t Baselines: Red %i, Green %i, Blue %i\r\n", r,g,b, rsenbase, gsenbase, bsenbase);
		set_green_led(50); _delay_ms(100); led_off(); _delay_ms(500);
	}
}